package com.warrantyhotel.android.adapter;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.GetFreeTable;
import com.warrantyhotel.android.util.BitmapUtils;
import com.warrantyhotel.android.util.BitmapUtils.OnFetchCompleteListener;

public class FreeCreditAdapter extends CursorAdapter {

	private LayoutInflater mInflater;

	public FreeCreditAdapter(Context context, Cursor c) {
		super(context, c, false);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		final RssItemViewHolder listItem = (RssItemViewHolder) view.getTag();
		final String FreeTitle = cursor.getString(cursor.getColumnIndex(GetFreeTable.ACTION_TITLE));
		final String description = cursor.getString(cursor.getColumnIndex(GetFreeTable.ACTION_TEXT));
		final String thumbnail = cursor.getString(cursor.getColumnIndex(GetFreeTable.THUMBNAIL_LINK));
		final int credit = cursor.getInt(cursor.getColumnIndex(GetFreeTable.AWARD_CREDITS));
		final int upload = cursor.getInt(cursor.getColumnIndex(GetFreeTable.AWARD_UPLOADS));

		if (0 != credit) {
			listItem.txtEarnCredit.setVisibility(View.VISIBLE);
			listItem.txtEarnCredit.setText(context.getString(R.string.earn) + " " + credit + " " + context.getString(R.string.credits));
		}

		if (0 != upload) {
			listItem.txtEarnUpload.setVisibility(View.VISIBLE);
			listItem.txtEarnUpload.setText(context.getString(R.string.earn) + " " + upload + " " + context.getString(R.string.uploads));
		}

		listItem.txtFreeTitle.setText(FreeTitle);

		BitmapUtils.fetchImage(context, thumbnail, new OnFetchCompleteListener() {

			@Override
			public void onFetchComplete(Object cookie, Bitmap result) {
				listItem.FreeImage.setImageBitmap(result);
			}
		});

		listItem.txtFreeDescription.setText(description.substring(0, 50));
		listItem.txtFreeDescription.append("...");
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = mInflater.inflate(R.layout.get_free_item, null);
		final RssItemViewHolder listItem = new RssItemViewHolder();
		listItem.txtFreeTitle = (TextView) view.findViewById(R.id.get_free_title_item);
		listItem.txtFreeDescription = (TextView) view.findViewById(R.id.get_free_description_item);
		listItem.FreeImage = (ImageView) view.findViewById(R.id.get_free_thumbnail_item);
		listItem.txtEarnCredit = (TextView) view.findViewById(R.id.earn_credits_item);
		listItem.txtEarnUpload = (TextView) view.findViewById(R.id.earn_uploads_item);
		view.setTag(listItem);
		return view;
	}

	public class RssItemViewHolder {
		TextView txtFreeTitle;
		TextView txtFreeDescription;
		TextView txtEarnCredit;
		TextView txtEarnUpload;
		ImageView FreeImage;
	}

}
